package graphic.dialogs;

import graphic.FilesTabbedPanel;
import graphic.MainFrame;
import graphic.mycomponents.TextChecked;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import utility.GraphicUtilsFactory;
import core.Function;

public class NewProgramDialog extends AbstractDialog
{
	TextChecked programName;
	TextChecked extenzion;
	TextChecked newFilePath;
	
	JButton selectFile;
	
	public NewProgramDialog()
	{	
		super("Add New Program", "Add Program", 450, 250);
	}
	
	public void createPanel()
	{
		super.setPanel(new JPanel(new GridBagLayout()));
		
		JPanel panel = super.getPanel(); 
		
		this.programName = new TextChecked("", TextChecked.TEXT, true, false, Function.getProgramNamesList());
		this.extenzion = new TextChecked("", TextChecked.TEXT, true, false, null);
		this.newFilePath = new TextChecked("", TextChecked.FILE, true, false, null);
		
		GraphicUtilsFactory.setTitleAndBorderToComponent(this.programName, "Program Name");
		GraphicUtilsFactory.setTitleAndBorderToComponent(this.extenzion, "Program Extenzion");		
		GraphicUtilsFactory.setTitleAndBorderToComponent(this.newFilePath, "New File Template Path");		
		
		/*
		this.newFilePath.addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent arg0) {}
			
			@Override
			public void focusGained(FocusEvent arg0)
			{
				if(!NewProgramDialog.this.newFilePath.getText().equals("")) return;
				JFileChooser chooser = new JFileChooser();
		        int returnVal = chooser.showOpenDialog(NewProgramDialog.this);

		        if (returnVal == JFileChooser.APPROVE_OPTION)
		            NewProgramDialog.this.newFilePath.setText(chooser.getSelectedFile().getPath());
		        else NewProgramDialog.this.extenzion.requestFocus();
			}
		});
		*/
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.CENTER;
		c.insets = new Insets(5, 10, 5, 10);
		
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 2;
		panel.add(new JLabel("Program Adding"), c);
		
		c.gridx = 0;
		c.gridy = 1;
		panel.add(this.programName, c);		

		c.gridx = 0;
		c.gridy = 2;
		panel.add(this.extenzion, c);
		
		c.gridx = 0;
		c.gridy = 3;
		panel.add(this.newFilePath, c);
		
		c.gridx = 0;
		c.gridy = 4;
		c.gridwidth = 1;
		panel.add(super.getOkButton(), c);

		c.gridx = 1;
		c.gridy = 4;
		panel.add(super.getExitButton(), c);
	}

	@Override
	public boolean checkOkFunction()
	{
		// TODO: FILE PATH ????
		
		if(!this.programName.isChecked())
		{
			JOptionPane.showMessageDialog(this, "Please type a correct program name!", "Problem occurred adding a program!", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		
		if(!this.extenzion.isChecked())
		{
			JOptionPane.showMessageDialog(this, "Please type a correct extenzion!", "Problem occurred adding a program!", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		
		return true;
	}

	@Override
	public void okFunction()
	{
		if(Function.addProgram(this.programName.getText(), this.extenzion.getText(), this.newFilePath.getText()))
		{
			super.dispose();
			MainFrame.getInstance().showMessage("New Program Added!", JOptionPane.INFORMATION_MESSAGE);
			FilesTabbedPanel.getInstance().addProgram(this.programName.getText());
		} else {
			super.dispose();
			MainFrame.getInstance().showMessage("New Program NOT Added!", JOptionPane.ERROR_MESSAGE);
		}		
	}
}